package view;

import database.Database;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Model;
import model.UserPage;

/**
 * UserPageHTML implements the HTML based user dashboard page view handling of the Lithub
 * website, which displays all of a user's account and inventory information.
 *
 * Primarily used for the web-based side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class UserPageHTML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * UserPageHTML(...) is a reciever of user dashboard page view requests and
     * also the responder to such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    UserPageHTML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of UserPage
     * model implementation to try to provide the proper dashboard information
     * of the requesting user.
     * 
     * On success, the user's user dashboard is dispalyed.
     *
     * @param a model which refers to the model that implements the UserPage
     * page.
     *
     * @catch generic exception.
     *
     */
    @Override
    public void invoke(Model m) {
        PrintWriter out = null;
        try {
            if (((UserPage) m).requiresRedirect) {
                response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302
                response.sendRedirect("index.jsp?model=UserPage&view=UserPageHTML&action=ShowUserPage");
            } else {
                request.setAttribute("model", m);
                out = response.getWriter();
                RequestDispatcher view = request.getRequestDispatcher("userPage.jsp");
                view.forward(request, response);
            }
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
